<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
	public function __construct(){
        parent::__construct();
        $this->admin = M('admin');
    }
    /*
    title : 登录页面
	time : 2018-05-08 9:40
	username : 安林
     */
	public function login()
	{
		if(IS_POST){
			$data = I('post.arr');
			$data['password'] = md5($data['password']);
			$admin = $this->admin->where($data)->find();
			if($admin){
				session('admin_id',$admin['admin_id']);
				$res = $this->admin->where(array('admin_id'=>$admin['admin_id']))->save(array('last_login'=>time(),'last_ip'=>get_client_ip()));
				$this->success('登录成功','/index');
			}else{
				$this->error('登录失败(错误代码1)！',U('Login/login'));
			}
		}else{
			session('admin_id',null);
		}
		$this->display();
	}
	/*
    title : 注册页面
	time : 2018-05-08 10:20
	username : 安林
     */
	public function register()
	{
		if(IS_POST){
			$data = I('post.arr');
			//判断用户是否勾选注册协议
			if(!$data['is_xy']){
				$this->error('请勾选协议！',U('Login/register'));
			}
			$password2 = I('post.password2');
			//判断密码是否一致
			if($password2 != $data['password']){
				$this->error('密码不正确！',U('Login/register'));
			}else{
				//对密码进行MD5加密
				$data['add_time'] = time();
				$data['password'] = md5($data['password']);
				$res = $this->admin->add($data);
				if($res){
					$this->success('注册成功！',U('Login/login'));
				}else{
					$this->error('注册失败！(错误代码2)',U('Login/register'));
				}
			}
		}
		$this->display();
	}

}